package main

import (
	"bufio"
	"fmt"
	"io"
	"io/ioutil"
	"os"
)

/*
	1. 带缓冲流的读取数据
		func NewReader(rd io.Reader) *Reader 缓冲区 65536
		read methods:
			func (b *Reader) Read(p []byte) (n int, err error)
			func (b *Reader) ReadSlice(delim byte) (line []byte, err error)
			func (b *Reader) ReadBytes(delim byte) (line []byte, err error)
			func (b *Reader) ReadString(delim byte) (line string, err error)
	2. 一次性读取整个文件， 适合小文件
		func ReadFile(filename string) ([]byte, error)
	3. os包下read方法
		func (f *File) Read(b []byte) (n int, err error)

*/
func main() {
	file, err := os.Open("../interface/test1.go")
	if err != nil {
		fmt.Println(nil)
	}
	defer file.Close()

	rd := bufio.NewReader(file)
	for {
		// str, er := rd.ReadString('\n') // string
		slice, er := rd.ReadSlice('\n') // []byte
		// bytes, er := rd.ReadBytes('\n') // []byte
		if er == io.EOF {
			break
		}
		fmt.Print(string(slice))
	}
	fmt.Println("-------------------------------")
	content, err3 := ioutil.ReadFile("./test.dat")
	if err3 != nil {
		fmt.Println(err3)
	}
	fmt.Println("content is\n", string(content))
	fmt.Println("-------------------------------")

	f2, err4 := os.Open("./test.dat")
	if err4 != nil {
		fmt.Println(err4)
	}
	defer f2.Close()
	for bytes := make([]byte, 1024); ; {
		if _, err := f2.Read(bytes); err == io.EOF {
			break
		} else {
			fmt.Println(string(bytes))
		}
	}
}
